package com.application.Activity;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.application.Adapter.RuleAdapter;
import com.application.Controller.ActivityController;
import com.application.Controller.ReadWriteFileController;
import com.application.DTO.RoleDTO;
import com.application.PhanQuyen.Quyen;
import com.application.RowDTO.RuleRowDTO;


public class frmTaoRoleActivity extends Activity {
	ListView lvDanhSachRule;
	Button btnChapNhan;
	Button btnTaoVaiTro;
	TextView txtTenRole;
	public ArrayList<RuleRowDTO> data;
	RoleDTO oldRole = null;
	// RoleDTO oldRole=null;
	Handler hander = new Handler();
	String path = ReadWriteFileController.ROLE_SETTING_PATH;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_tao_role);
		txtTenRole = (TextView) findViewById(R.id.txtTenVaiTro);
		btnTaoVaiTro = (Button) findViewById(R.id.btnTaoVaiTro);
		lvDanhSachRule = (ListView) findViewById(R.id.lvDanhSachRule);
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			oldRole = (RoleDTO) bundle.getSerializable("Update");
			if (oldRole != null) {
				txtTenRole.setText(oldRole.getName());
				btnTaoVaiTro.setText("Cập nhật");
			}
		}
		fillListView();
		btnTaoVaiTro.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				RoleDTO newRole = createRole();
				if (newRole == null) {
					showErrorAlert("Bạn phải chọn ít nhất 1 quyền");
				} else if (newRole.getName().length() <= 0) {
					showErrorAlert("Bạn phải nhập tên vai trò!");

				} else {
					if (oldRole == null) {
						Log.d("Phu", "insertRole role");
						ReadWriteFileController.insertRole(path, newRole);
					} else {
						Log.d("Phu", "update role");
						ReadWriteFileController.updateRole(path, oldRole, newRole);
					}
					finish();
				}

			}
		});
	}

	private void showToast(String text) {
		Toast t = Toast.makeText(getBaseContext(), text, Toast.LENGTH_SHORT);
		t.show();
	}

	public void fillListView() {
		ArrayList<Quyen> ruleList;

		ruleList = ReadWriteFileController.readRule(getBaseContext(),ReadWriteFileController.RULE_BASE_PATH);
		data = new ArrayList<RuleRowDTO>();
		for (Quyen rule : ruleList) {
			RuleRowDTO row = new RuleRowDTO();
			row.rule = rule;
			if (oldRole != null) {
				int n = oldRole.getRuleList().size();
				for (int i = 0; i < n; i++) {
					if (oldRole.getRuleList().get(i).getID().equals(rule.getID())) {
						row.check = true;
						break;
					}
				}
			}
			data.add(row);
		}
		RuleAdapter adapter = new RuleAdapter(this, data);
		lvDanhSachRule.setAdapter(adapter);
	}

	public int countSelectItem() {
		int n = data.size();
		int count = 0;
		for (int i = 0; i < n; i++) {
			if (data.get(i).check)
				count++;
		}
		return count;
	}

	private void showErrorAlert(String text) {
		Log.d("phu", "null mat roi");
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		Log.d("phu", "AlertDialog.Builder alert");
		alert.setTitle("Thông báo");
		alert.setIcon(R.drawable.alerts_error);
		alert.setMessage(text);
		alert.setPositiveButton("Đồng ý", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				return;
			}
		});
		Log.d("phu", "toi day chưa");
		alert.show();

	}

	private RoleDTO createRole() {
		ArrayList<Quyen> rs = new ArrayList<Quyen>();
		int n = data.size();
		for (int i = 0; i < n; i++) {
			if (data.get(i).check) {
				rs.add(data.get(i).rule);
			}

		}

		if (rs.size() == 0)
			return null;
		RoleDTO role = new RoleDTO();
		role.setName(txtTenRole.getText().toString());
		role.setRuleList(rs);

		return role;
	}
	@Override
	public void onStart(){
		super.onStart();
		ActivityController.setPeerActivity(this);
	}
	@Override
	public void onResume(){
		super.onResume();
		ActivityController.setPeerActivity(this);
	}
	@Override
	public void onPause(){
		super.onPause();
		ActivityController.setPeerActivity(this);
	}
}
